package com.my12306.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.constant.CommonConstants;
import com.my12306.common.enums.CodeEnum;
import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.dao.ZoneDao;
import com.my12306.train.model.entity.ZoneEntity;
import com.my12306.train.model.vo.ZoneVO;
import com.my12306.train.service.ZoneService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;


@Service("zoneService")
public class ZoneServiceImpl extends ServiceImpl<ZoneDao, ZoneEntity> implements ZoneService {
    @Resource
    ZoneDao zoneDao;

    public PageUtils find(ZoneVO zoneVO, long page, long size) {
        Page<ZoneEntity> page1 = new Page<>(page, size);
        QueryWrapper<ZoneEntity> qs = new QueryWrapper<>();
        if (zoneVO == null) {
            return new PageUtils(zoneDao.selectPage(page1, qs));
        }
        if (zoneVO.getZoneFrom() != null) {
            qs.like("zone_from", zoneVO.getZoneFrom());
        }
        if (zoneVO.getZoneTo() != null) {
            qs.like("zone_to", zoneVO.getZoneTo());
        }
        return new PageUtils(zoneDao.selectPage(page1, qs));
    }

    @Override
    public PageUtils queryZoneByPage(PageVo pageVo) {
        pageVo.init();
        List<ZoneEntity>zoneEntities = this.baseMapper.queryZoneByPage(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        int totalCount  = this.queryZoneCount(pageVo, CommonConstants.LogicDelete.no_delet.getValue());
        PageUtils pageUtils = PageUtils.build(pageVo, zoneEntities, totalCount);
        return pageUtils;
    }

    @Override
    public List<ZoneVO> queryAllZone(int logicDelete) {
       return  this.baseMapper.queryAllZone(logicDelete);
    }

    @Override
    public ZoneEntity queryZoneMinByZoneFrom() {
     return   this.baseMapper.queryZoneMinByZoneFrom(CommonConstants.LogicDelete.no_delet.getValue());
    }

    private Integer queryZoneCount(PageVo pageVo, Integer logicDelete) {
      return   this.baseMapper.queryZoneCount(pageVo,logicDelete);
    }


    @Override
    public ResponseData saveZone(ZoneEntity zone) {
        if (zone!=null){
            zone.setId(SnowflakeIdWorker.generateId());
            zone.setCreateDate(new Date());
            zone.setUpdateDate(new Date());
            Double zoneTo = zone.getZoneTo();
            @NotNull Double zoneFrom = zone.getZoneFrom();
            if (zoneFrom!=null) {
                double distance= 0;
                if (zoneTo!=null) {
                     distance = zoneTo - (zoneFrom - 1);
                    zone.setZoneAmount(distance/zone.getZoneDistance());

                }else{
                    zone.setZoneTo(-1D);
                    zone.setZoneAmount((zoneFrom-1)/zone.getZoneDistance());
                }

            }
            this.baseMapper.insert(zone);
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.ADD_FAIL.getCode(),"添加失败！原因：添加的数据不能为空!");
    }

    @Override
    public void removeZoneByIds(List<Long> ids) {
        if (ids!=null){
            this.baseMapper.removeZoneByIds(ids,CommonConstants.LogicDelete.delete.getValue());
        }

    }

    @Override
    public ResponseData updateZoneById(ZoneEntity zone) {
        if (zone!=null&&zone.getId()!=null){
            zone.setUpdateDate(new Date());
            Double zoneTo = zone.getZoneTo();
            @NotNull Double zoneFrom = zone.getZoneFrom();
            if (zoneFrom!=null) {
                double distance= 0;
                if (zoneTo!=null) {
                    distance = zoneTo - (zoneFrom - 1);

                    zone.setZoneAmount(distance/zone.getZoneDistance());

                }else{
                    zone.setZoneTo(-1D);
                    zone.setZoneAmount((zoneFrom-1)/zone.getZoneDistance());
                }
            }
            this.baseMapper.updateById(zone);
        }
        return ResponseData.fail(CodeEnum.ADD_FAIL.getCode(),"修改失败！原因：修改的数据不合法!");
    }

    @Override
    public ZoneEntity queryZoneById(long id) {
        QueryWrapper<ZoneEntity> qw = new QueryWrapper<>();
        qw.eq("id",id).eq("logic_delete",CommonConstants.LogicDelete.no_delet.getValue());
       return this.baseMapper.selectOne(qw);
    }
}
